package cn.edu.wfit.modules.learn.service.impl.coursemangement;

import java.util.List;

import cn.edu.wfit.common.core.utils.DateUtils;
import cn.edu.wfit.modules.learn.domain.WfitStuCourse;
import cn.edu.wfit.modules.learn.mapper.coursemangement.WfitStuCourseMapper;
import cn.edu.wfit.modules.learn.service.coursemangement.IWfitStuCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程学生关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-18
 */
@Service
public class WfitStuCourseServiceImpl implements IWfitStuCourseService {
    @Autowired
    private WfitStuCourseMapper wfitStuCourseMapper;

    /**
     * 查询课程学生关联
     *
     * @param id 课程学生关联主键
     * @return 课程学生关联
     */
    @Override
    public WfitStuCourse selectWfitStuCourseById(Long id) {
        return wfitStuCourseMapper.selectWfitStuCourseById(id);
    }

    /**
     * 查询课程学生关联列表
     *
     * @param wfitStuCourse 课程学生关联
     * @return 课程学生关联
     */
    @Override
    public List<WfitStuCourse> selectWfitStuCourseList(WfitStuCourse wfitStuCourse) {
        return wfitStuCourseMapper.selectWfitStuCourseList(wfitStuCourse);
    }

    /**
     * 新增课程学生关联
     *
     * @param wfitStuCourse 课程学生关联
     * @return 结果
     */
    @Override
    public int insertWfitStuCourse(WfitStuCourse wfitStuCourse) {
        List<WfitStuCourse> list = wfitStuCourseMapper.selectWfitStuCourseList(wfitStuCourse);
        if (!list.isEmpty()) {
            //已申请，抛出异常
            throw new RuntimeException("您已申请过此课程，无须再次申请");
        }else {
            return wfitStuCourseMapper.insertWfitStuCourse(wfitStuCourse);
        }
    }

    /**
     * 修改课程学生关联
     *
     * @param wfitStuCourse 课程学生关联
     * @return 结果
     */
    @Override
    public int updateWfitStuCourse(WfitStuCourse wfitStuCourse) {
        //如果加入了课程，设置加入时间
        if(wfitStuCourse.getIsJoined().equals("1")){
            wfitStuCourse.setJoinDate(DateUtils.getNowDate());
        }
        return wfitStuCourseMapper.updateWfitStuCourse(wfitStuCourse);
    }

    /**
     * 批量删除课程学生关联
     *
     * @param ids 需要删除的课程学生关联主键
     * @return 结果
     */
    @Override
    public int deleteWfitStuCourseByIds(Long[] ids) {
        return wfitStuCourseMapper.deleteWfitStuCourseByIds(ids);
    }

    /**
     * 删除课程学生关联信息
     *
     * @param id 课程学生关联主键
     * @return 结果
     */
    @Override
    public int deleteWfitStuCourseById(Long id) {
        return wfitStuCourseMapper.deleteWfitStuCourseById(id);
    }
}
